package com.itheima.api;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.mapper.UmsMenuMapper;
import com.itheima.pojo.UmsMenu;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@DubboService
public class UmsMenuApiImpl implements UmsMenuApi{

    @Autowired
    private UmsMenuMapper umsMenuMapper;

    /**
     * 根据菜单id查询菜单集合
     * @param menuIds
     * @return
     */
    @Override
    public List<UmsMenu> findMenus(List<Long> menuIds) {
        return umsMenuMapper.selectBatchIds(menuIds);
    }

    /**
     * 根据id查询菜单
     * @param id
     * @return
     */
    @Override
    public UmsMenu findById(Integer id) {
        return umsMenuMapper.selectById(id);
    }

    /**
     * 根据id修改菜单
     * @param id
     * @param umsMenu
     */
    @Override
    public void updateById(Integer id, UmsMenu umsMenu) {
        LambdaQueryWrapper<UmsMenu> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UmsMenu::getId,id);
        umsMenuMapper.update(umsMenu,wrapper);
    }

    @Override
    public void updateHidden(Integer id, Integer hidden) {
        LambdaQueryWrapper<UmsMenu> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(UmsMenu::getId,id);
        UmsMenu umsMenu = new UmsMenu();
        umsMenu.setHidden(hidden);
        umsMenuMapper.update(umsMenu,wrapper);
    }



    /**
     * 添加后台菜单
     */
    @Override
    public void create(UmsMenu umsMenu) {
        mapper.insert(umsMenu);
    }


    /**
     * 根据ID删除后台菜单
     */
    @Override
    public void delete(List<Long> idList) {
        mapper.deleteBatchIds(idList);
    }


    /**
     * 获取指定菜单的子菜单id
     */
    @Override
    public List<Long> getChildId(Long id) {
        // 构建查询
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UmsMenu::getParentId, id);
        // 获取当前id所对应目录的子目录列表
        List<UmsMenu> menuList = mapper.selectList(queryWrapper);
        if(menuList.size() > 0) {
            // 若存在子目录
            // 提取子目录id
            List<Long> idList = CollUtil.getFieldValues(menuList, "parentId", Long.class);
            List<Long> temp = new ArrayList<>();
            for (Long tid : idList) {
                // 遍历子目录id, 获取子目录的子目录id列表并添加到临时集合中
                temp.addAll(getChildId(tid));
            }
            // 将临时列表中的元素加入idList并返回
            idList.addAll(temp);
            return idList;
        }else{
            // 不存在子目录, 直接返回空集合
            return Collections.emptyList();
        }
    }


    /**
     * 分页查询后台菜单
     */
    @Override
    public Page<UmsMenu> page(Long parentId, Integer pageNum, Integer pageSize) {
        Page<UmsMenu> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<UmsMenu> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(parentId != null, UmsMenu::getParentId, parentId);
        return mapper.selectPage(pageInfo, queryWrapper);
    }

    @Override
    public List<UmsMenu> list() {
        return mapper.selectList(null);
    }
}
